<template>
	<view class="page">
		<view class="box-c">
			<view class="" v-if="loginState">
				<view v-if="loginState" class="box list d-jc-sb d-ai-c" @tap='handleUserInfo'>
					<view class="">
						个人信息
					</view>
					<view class="iconfont iconqianjin right">

					</view>
				</view>
				<view class="line">

				</view>
			</view>
			<view class="" v-if="loginState">
				<view class="box list d-jc-sb d-ai-c" >
					<view class="">
						意见反馈
					</view>
					<view class="iconfont iconqianjin right">

					</view>
				</view>
				<view class="line">

				</view>
			</view>
			
			<view class="" >
				<view class="box list d-jc-sb d-ai-c">
					<view class="">
						清除缓存
					</view>
					<view class="box right" @tap='handleClearCache'>
						<view class="">
							{{fileSizeString}}
						</view>
						<view class="iconfont iconqianjin">

						</view>
					</view>
				</view>
				<view class="line">

				</view>
			</view>
			<view class="">
				<view class="box list d-jc-sb d-ai-c">
					<view class="">
						当前版本
					</view>
					<view class="box right">
						<view class="">
							{{appversion}}
						</view>
						<view class="iconfont iconqianjin">

						</view>
					</view>

				</view>
				<view class="line">

				</view>
			</view>
			<view class="">
				<view class="box list d-jc-sb d-ai-c">
					<view class="">
						用户协议
					</view>
					<view class="iconfont iconqianjin right">

					</view>
				</view>
				<view class="line">

				</view>
			</view>
		</view>
		<view class="loginout" @tap='bindLogin' v-if="loginState">
			退出登录
		</view>




		<!-- <button type="default" @tap='bindLogin'>退出</button> -->



	</view>


</template>

<script>
	import hansTabber from '../../components/hans-tabbar/hans-tabbar.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		props: {
			visible: Boolean
		},
		components: {
			hansTabber
		},
		computed: mapState(['loginState', 'userInfo']),
		data() {
			return {
				current1: 3,
				user: {},
				testdata: {},
				getUserIn: '',
				top: 0,
				fileSizeString: "", // 缓存总数
				appversion:''

			}
		},
		onShow() {

		},
		onLoad() {
			if (!uni.getStorageSync("isBang")) {
				this.top = 20
			}
			//#ifdef APP-PLUS
			this.formatSize()
			//#endif

		},
		methods: {
			...mapMutations(['logout', 'setUserInfo']),
			handleUserInfo(){
				uni.navigateTo({
					url:'userEdit'
				})
			},
			formatSize() {
				this.appversion = uni.getStorageSync('appversion');
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			handleClearCache(){
				
				//#ifdef APP-PLUS
				let _this = this
				uni.showModal({
					title: '清除缓存',
					content: '缓存是使用过程中产生的临时数据，并不影响养老大街的正常使用。',
					success: function(res) {
						if (res.confirm) {
							_this.clearCache()
						} else if (res.cancel) {
							
						}
						
						
					}
				});
				
				//#endif
				
				
			},
			clearCache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径 
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录 
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize(); // 重新计算缓存 
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
				} else { // ios 
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						that.formatSize();
					});
				}
			},

			bindLogin() {
				let _this = this
				uni.showModal({ 
					title: '退出登录',
					content: '确认退出当前账号？',
					success: function(res) {
						if (res.confirm) {
							_this.logout()
							uni.navigateBack(1)
							//#ifdef APP-PLUS
							plus.runtime.setBadgeNumber(0);  
							//#endif
						} else if (res.cancel) {
							uni.showToast({
								title: '已取消',
								duration: 2000,
								icon: "none"
							})
						}
					}
				});

				// this.getUserInfo()
			},




		}
	}
</script>

<style lang="less" scoped>
	.page {
		background-color: #F8F8F8;
		height: 100%;
	}

	.loginout {
		width: 600rpx;
		height: 88rpx;
		background-color: #FF5551;
		margin: 0 auto;
		line-height: 88rpx;
		text-align: center;
		color: #fff;
		font-size: 32rpx;
		border-radius: 50rpx;
		margin-top: 100rpx;
	}

	.iconqianjin {
		margin-left: 20rpx;
	}

	.list {
		padding: 0 20rpx;
		height: 100rpx;
		background-color: #FFFFFF;
		font-size: 30rpx;
		color: #333333FF;

		.right {
			color: #999999FF;
		}
	}

	.line {
		width: 100%;
		height: 4rpx;
		background-color: #F8F8F8;
	}
</style>
